﻿using System;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using YTE.Control;
using 右下角弹窗.DiagShow;

namespace LD.Host
{
    public partial class FrmHDEdit : FrmOKCancel
    {
        private string m_name;
        public FrmHDEdit()
        {
            InitializeComponent();
            UI();
            this.Shown += FrmEdit_Shown;
            this.btn_ok.Click += Btn_ok_Click;
            this.txt_heartbeat.Text = "";
        }
        public FrmHDEdit(string name) : this()
        {
            m_name = name;
            this.txt_name.Text = m_name;
            var isExist = CommunicateFactory.Instance.GetList.Where(p => p.Name == this.txt_name.Text).FirstOrDefault();
            if (isExist != null)
            {
                //this.txt_name.ReadOnly = true;
                this.txt_address.Text = isExist.Address;
                this.txt_port.Text = isExist.Port + "";
                this.txt_timeOut.Text = isExist.TimeOut + "";
                this.cob_hdType.SelectedIndex = isExist.HDType;
                this.cob_parity.SelectedIndex = isExist.Parity;
                this.cob_plc.SelectedIndex = isExist.Model;
                this.txt_heartbeat.Text = isExist.Heartbeat;
            }
        }
        private void FrmEdit_Shown(object sender, EventArgs e)
        {

        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            if (CommunicateFactory.Instance.GetList == null)
                CommunicateFactory.Instance.GetList = new System.Collections.Generic.List<TB_HD>();
            Dialog dialog = new Dialog();
            if (!int.TryParse(this.txt_port.Text, out var port))
            {
                dialog.Warning("端口数据类型错误");
                return;
            }
            if (!int.TryParse(this.txt_timeOut.Text, out var timeout))
            {
                dialog.Warning("超时数据类型错误");
                return;
            }
            if (string.IsNullOrEmpty(txt_name.Text))
            {
                dialog.Warning("名称不能为空");
                return;
            }

            //检查数据
            var isExist = CommunicateFactory.Instance.GetList.Where(p => p.Name == this.txt_name.Text).FirstOrDefault();
            if (isExist != null)
            {
                isExist.Address = this.txt_address.Text;
                isExist.Port = port;
                isExist.TimeOut = timeout;
                isExist.HDType = this.cob_hdType.SelectedIndex;
                isExist.Parity = this.cob_parity.SelectedIndex;
                isExist.Model = this.cob_plc.SelectedIndex;
                isExist.Heartbeat = this.txt_heartbeat.Text;
            }
            else
            {
                TB_HD hd = new TB_HD()
                {
                    Name = this.txt_name.Text,
                    Address = this.txt_address.Text,
                    Port = port,
                    TimeOut = timeout,
                    HDType = this.cob_hdType.SelectedIndex,
                    Parity = this.cob_parity.SelectedIndex,
                    Model = this.cob_plc.SelectedIndex,
                    Heartbeat = this.txt_heartbeat.Text
                };
                CommunicateFactory.Instance.GetList.Add(hd);
            }
            this.Close();
        }

        private void cob_hdType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (this.cob_hdType.Text == "PLC")
            //{
            //    this.Size = new Size(793, 363);
            //}
            //else
            //    this.Size = new Size(550, 363);

        }
        private void UI()
        {
            foreach (var item in Enum.GetNames(typeof(HDType)))
            {
                this.cob_hdType.Items.Add(item);
            };
            foreach (var item in Enum.GetNames(typeof(Parity)))
            {
                this.cob_parity.Items.Add(item);
            };
            foreach (var item in Enum.GetNames(typeof(PLCType)))
            {
                this.cob_plc.Items.Add(item);
            };
            //this.cob_hdType.SelectedIndex = 0;

            //DataTable xieyi = Get协议();
            //this.cob_hdType.DataSource = xieyi;
            //this.cob_hdType.DisplayMember = "name";
            //this.cob_hdType.ValueMember = "id";

            //DataTable jiaoyan = Get效验();
            //this.cob_parity.DataSource = jiaoyan;
            //this.cob_parity.DisplayMember = "name";
            //this.cob_parity.ValueMember = "id";

            //DataTable plc = GetPLC();
            //this.cob_plc.DataSource = plc;
            //this.cob_plc.DisplayMember = "name";
            //this.cob_plc.ValueMember = "id";
        }
        private DataTable Get协议()
        {
            string[] str = Enum.GetNames(typeof(HDType)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private DataTable Get效验()
        {
            string[] str = Enum.GetNames(typeof(Parity)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private DataTable GetPLC()
        {
            string[] str = Enum.GetNames(typeof(PLCType)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
    }
}
